package com.woniuxy.Cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * @auther linke
 * @create 2021-10-21 14:28
 */
@WebServlet("/cookieDemo02")
public class CookieDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cookie对象
        Cookie[] cookies = req.getCookies();//Cookie: name=tom; Idea-ccd6a49b=669dffcf-1a86-4adc-9eab-1e64a6c2c36e
        for (Cookie cookie : cookies) {
            System.out.println(cookie);//javax.servlet.http.Cookie@2ffc1ece
            System.out.println(cookie.getName()+"..."+cookie.getValue());//name...tom
            //Cookie: name=%E5%BC%A0%E4%B8%89; Idea-ccd6a49b=669dffcf-1a86-4adc-9eab-1e64a6c2c36e
            System.out.println(URLDecoder.decode(cookie.getName()+"..."+cookie.getValue(), "utf-8"));//name...张三
            //中文编码格式  直接设置头的方式需要进行编码和解码
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
